<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login">
    <div class="login-con">
      <Card icon="log-in" title="欢迎登录" :bordered="false">
        <div class="form-con">
          <login-form ref="loginForm" @on-success-valid="handleSubmit"></login-form>
          <!-- <p class="login-tip">提示</p> -->
        </div>
      </Card>
    </div>
  </div>
</template>

<script>

import LoginForm from '_c/login-form'
import { mapActions } from 'vuex'
export default {
  components: {
    LoginForm
  },
  methods: {
    ...mapActions([
      'handleLogin',
      'getUserInfo',
      'getRouters'
    ]),
    handleSubmit ({ userName, password, code }) {
      this.handleLogin({ userName, password, code }).then(res => {
        if (res.code === 200) {
          this.getUserInfo().then(res => {
            if (res.code === 200) {
              this.$Message.success('登录成功')
              this.$router.push({
                name: this.$config.homeName
              })
            } else {
              this.$Message.error(res.error)
            }
          }).catch(err => {
            console.log(err)
            this.$Message.error(res.error)
          })
        } else {
          // this.$refs.loginForm.fresh_code()
          this.$Message.error(res.error)
        }
      }).catch(err => {
        console.log(err)
        // this.$refs.loginForm.fresh_code()
        this.$Message.error('登录失败')
      })
    }
  }
}
</script>

<style>

</style>
